package com.young.common.core.dal;

import com.young.common.core.dal.configuration.DalConfiguration;
import com.young.common.core.dal.service.IDataAccessService;
import com.young.common.core.dal.service.impl.DataAccessServiceImpl;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 数据执行服务自动配置
 * @author wangjiyu@imdada.cn
 * @create 2018/12/27
 */
@Configuration
@ConditionalOnProperty(
        name = "young.dal.enable-auto-config",
        havingValue = "true"
)
@EnableConfigurationProperties(DalConfiguration.class)
public class DalAutoConfigure {

    @Autowired
    DalConfiguration dalConfiguration;

    @Bean
    @ConditionalOnMissingBean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource, ResourceLoader resourceLoader) throws IOException {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setConfigLocation(resourceLoader.getResource(dalConfiguration.getConfigLocation()));
        PathMatchingResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
        List<Resource> resourceList = new ArrayList<Resource>();
        for (String location : dalConfiguration.getMapperLocations()) {
            Resource[] resources = resourceResolver.getResources(location);
            if (resources != null && resources.length > 0) {
                resourceList.addAll(Arrays.asList(resources));
            }
        }
        Resource[] resources = resourceList.toArray(new Resource[resourceList.size()]);
        sqlSessionFactoryBean.setMapperLocations(resources);
        return sqlSessionFactoryBean;
    }

    @Bean("sqlSessionTemplate")
    @ConditionalOnMissingBean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactoryBean sqlSessionFactoryBean) throws Exception {
        SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactoryBean.getObject());
        return sqlSessionTemplate;
    }

    @Bean("dataAccessService")
    @ConditionalOnMissingBean
    public IDataAccessService dataAccessService(SqlSessionTemplate sqlSessionTemplate) {
        DataAccessServiceImpl dataAccessService = new DataAccessServiceImpl();
        dataAccessService.setSqlSessionTemplate(sqlSessionTemplate);
        return dataAccessService;
    }
}
